# 直接使用优化后的Dockerfile.base进行构建，而不是引用外部base镜像
FROM docker.m.daocloud.io/python:3.11-slim AS builder

WORKDIR /app

ENV TIKTOKEN_CACHE_DIR=/app/vendor/tiktoken_cache
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV VIRTUAL_ENV=/app/venv/playwright
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# 使用官方Debian源
RUN echo "deb http://deb.debian.org/debian  bookworm main" > /etc/apt/sources.list && \
    echo "deb http://deb.debian.org/debian  bookworm-updates main" >> /etc/apt/sources.list && \
    echo "deb http://deb.debian.org/debian-security  bookworm-security main" >> /etc/apt/sources.list

# 安装最小化系统依赖
RUN apt-get clean && apt-get update --fix-missing && apt-get install -y --no-install-recommends \
    ca-certificates curl wget gnupg \
    # Playwright核心依赖
    libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 \
    libxdamage1 libxfixes3 libxrandr2 libgbm1 libpango-1.0-0 libcairo2 libasound2 \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# pip 使用国内源
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/  && \
    pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn

# 创建虚拟环境并升级pip（不保留缓存）
RUN python -m venv $VIRTUAL_ENV && \
    $VIRTUAL_ENV/bin/pip install --upgrade pip --no-cache-dir

# 拷贝并安装requirements（不保留缓存）
COPY requirements/base.txt requirements/dev.txt requirements/prod.txt /app/requirements/
RUN $VIRTUAL_ENV/bin/pip install -r requirements/base.txt --no-cache-dir

# 安装特定版本的Playwright浏览器以减少体积
RUN $VIRTUAL_ENV/bin/playwright install chromium --with-deps && \
    # 清理playwright缓存
    $VIRTUAL_ENV/bin/playwright uninstall-deps && \
    # 只保留必要的浏览器文件
    find $VIRTUAL_ENV -name "*.so*" -type f -exec strip --strip-debug {} \; 2>/dev/null || true

# 安装Node.js 18.x和npm
RUN apt-get update && apt-get install -y curl wget gnupg && \
    curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get update && apt-get install -y nodejs && \
    apt-get clean && rm -rf /var/lib/apt/lists/* && \
    # 验证安装
    node -v && \
    npm -v && \
    # 使用国内源并安装必要的npm包
    npm config set registry https://registry.npmmirror.com && \
    npm install -g --no-update-notifier --no-audit @playwright/mcp@0.0.44 && \
    # 验证@playwright/mcp安装
    npx @playwright/mcp --version

# 第二阶段：运行环境
FROM docker.m.daocloud.io/python:3.11-slim

WORKDIR /app

# 复制环境变量配置
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV VIRTUAL_ENV=/app/venv/playwright
ENV PATH="$VIRTUAL_ENV/bin:$PATH:/usr/local/bin:/usr/bin:/bin"
ENV INDOCKER=1
ENV DISPLAY=:99

# 安装运行时必需的系统依赖和基础命令包
RUN apt-get clean && apt-get update --fix-missing && apt-get install -y --no-install-recommends \
    ca-certificates curl wget gnupg libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 \
    libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 \
    libpango-1.0-0 libcairo2 libasound2 libx11-xcb1 libxcb-dri3-0 libxtst6 \
    # OpenCV (cv2) 所需的 OpenGL 库
    libgl1 libglib2.0-0 \
    # Xvfb虚拟显示服务器（用于无头浏览器）
    xvfb x11-xkb-utils xauth \
    # D-Bus支持（Chrome需要）
    dbus-x11 \
    # 基础命令包 - 文件操作和核心工具
    coreutils findutils util-linux \
    # 进程和网络工具
    procps net-tools iproute2 openssh-client \
    # 文本处理工具
    vim-tiny less sed grep gawk \
    # 压缩和解压工具
    tar gzip bzip2 xz-utils \
    # 其他常用工具
    bash bash-completion \
    # 中文字体支持
    fonts-wqy-zenhei fonts-wqy-microhei fonts-noto-cjk \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 安装Chromium浏览器（使用Debian官方源）
RUN apt-get update && apt-get install -y chromium chromium-driver && \
    apt-get clean && rm -rf /var/lib/apt/lists/* && \
    ln -s /usr/bin/chromium /usr/bin/chromium-browser && \
    ln -s /usr/bin/chromium /usr/bin/google-chrome-stable && \
    ln -s /usr/bin/chromium /usr/bin/google-chrome

# 从构建阶段复制虚拟环境
COPY --from=builder $VIRTUAL_ENV $VIRTUAL_ENV

# 在第二阶段直接安装Node.js和npm
RUN apt-get update && apt-get install -y curl wget gnupg && \
    curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get update && apt-get install -y nodejs && \
    apt-get clean && rm -rf /var/lib/apt/lists/* && \
    # 验证安装
    node -v && \
    npm -v && \
    # 使用国内源并安装必要的npm包
    npm config set registry https://registry.npmmirror.com && \
    npm install -g --no-update-notifier --no-audit @playwright/mcp@0.0.44 && \
    # 验证@playwright/mcp安装
    npx @playwright/mcp --version

# 创建符号链接
RUN ln -s /usr/lib/node_modules/@playwright/mcp/cli.js /usr/local/bin/mcp-cli

# 选择性拷贝应用代码和配置
COPY app/ /app/app/
COPY config/ /app/config/
COPY model/ /app/model/
COPY requirements/ /app/requirements/
COPY start_server.py /app/
COPY logger.py /app/
COPY vendor/ /app/
COPY start_mcp.py /app/
COPY start_mcp.sh /app/
COPY scripts/ /app/scripts/
COPY tests/ /app/tests/
COPY web/ /app/web/
COPY migrations/ /app/migrations/
COPY pyproject.toml /app/
COPY setup.cfg /app/
COPY wsgi.py /app/
COPY .env.example /app/

# 确保测试脚本有执行权限
RUN chmod +x /app/tests/*.sh && chmod +x /app/tests/*.py

# 创建必要目录
RUN mkdir -p logs/server logs/mcp_client logs/agent data/database data/tasks data/scenes data/screenshots data/user_data/user_default data/user_data/user_1 data/user_data/user_2 /app/test_reports

# 配置D-Bus
RUN mkdir -p /var/run/dbus && \
    chmod 755 /var/run/dbus

# 增加可执行权限等
RUN chmod +x start_server.py && chmod +x start_mcp.py && chmod +x start_mcp.sh

# hosts配置将在启动脚本中处理（运行时添加）

# 确保start_all.sh脚本有执行权限
RUN chmod +x /app/start_all.sh

# 安装dbus启动脚本
RUN echo '#!/bin/bash\nmkdir -p /var/run/dbus\nrm -f /var/run/dbus/pid\ndbus-daemon --system --fork' > /usr/local/bin/start-dbus.sh && \
    chmod +x /usr/local/bin/start-dbus.sh

EXPOSE 5228 8930 8931 8932

CMD ["/app/start_all.sh"]
